//
//  ArmyHandler.m
//  Legions
//
//  Created by Legions on 02/07/14.
//  Copyright (c) 2014 RefeSoft. All rights reserved.
//

#import "ArmyHandler.h"
#import "Soldier.h"
#import "MathUtilities.h"
#import "FollowTrackHandler.h"
#import "ArmySoldierFormation.h"

@implementation ArmyHandler{
    //these are INSTANCE variables - but I have read it is not the proper way to do it ... but wtf
    NSMutableArray          *soldierList;
    FollowTrackHandler      *followTrackHandler;
    ArmySoldierFormation    *armySoldierFormation;
}

//this is a global variable
SKNode *thisWorldNode;

float ARMYHANDLE_WIDTH  = 50;
float ARMYHANDLE_HEIGHT = 25;


-(instancetype) initWithParentNode : (SKNode*) parentNode position : (CGPoint) position{
    
    self                = [ArmyHandler spriteNodeWithImageNamed:@"Spaceship"]; // nothing before this - this is the initialization

    self.name           = @"handler";
    
    followTrackHandler  = [[FollowTrackHandler alloc] initWithMoveAbleGameObject : self];
    armySoldierFormation= [[ArmySoldierFormation alloc] initWithArmyHandler : self];
    
    soldierList         = [[NSMutableArray alloc] init];
    
    thisWorldNode       = parentNode;
    
        if(self){
            [self setSize:CGSizeMake(ARMYHANDLE_WIDTH, ARMYHANDLE_HEIGHT)];
            self.position   = position;
            self.alpha      = 1;

            [parentNode addChild:self];
        }

    return self;
}

- (void)move:(NSNumber *)dt{
    [followTrackHandler move:dt];
}

-(void) addPointToMove:(CGPoint)point{
    [followTrackHandler addPointToMove:point];
}

- (void) moveTo:(CGPoint)destination duration:(NSTimeInterval)sec{
   //   NSLog(@"after movemethod rotation %f", fmod(self.zRotation, 2*M_PI)*(180/M_PI));

    
    CGFloat rad = [MathUtilities angleBetween:self.position and:destination];
    float rotate = M_PI - fmod(self.zRotation, 2*M_PI) - rad; //start angle - current angle - new angle
    
    if(rotate > M_PI){
        rotate = rotate - 2*M_PI;
    }
    
    if (rotate < -M_PI) {
        rotate = rotate + 2*M_PI;
    }
    
    [self removeAllActions]; //this was not a good idee / lose of angle when multiple actions performed
   // [self runAction:[SKAction rotateByAngle:rotate duration:sec ]];
   // [self runAction:[SKAction moveTo:destination duration:sec]];
    
    SKAction *rotateAct = [SKAction rotateByAngle:rotate duration:sec/5 ];
    SKAction *moveAct = [SKAction moveTo:destination duration:sec];
    
    SKAction *sequence = [SKAction sequence:@[rotateAct, moveAct]];
    [self runAction: sequence];
    
    
    //for keeping position in army
    [[soldierList objectAtIndex:0] runAction:[SKAction moveTo:CGPointMake(10, -20) duration:sec ]];
    
   // NSLog(@"after movemethod rotation %f", fmod(self.zRotation, 2*M_PI)*(180/M_PI));

}

- (void) attack:(ArmyHandler<GameObjectType> *)armyHandle{
    
    //actually just a test to see if the soldier , child of armyhandler,- can convert worldNodeCoordinates
    //this way we dont have to put all in the world node - and I guess movement of armies will be more easy
    
    for (Soldier *soldier in soldierList) {
        
        CGPoint dest = [self convertPoint: armyHandle.position fromNode: thisWorldNode];
        [soldier moveTo:dest duration:2];
    }
}

@end
